home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / File / Fstab.php < prev    next >
Encoding:
PHP Script  |  2005-12-02  |  8.3 KB  |  303 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Main File_Fstab file
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category  File Formats
  16.  * @package   File_Fstab
  17.  * @author    Ian Eure <ieure@php.net>
  18.  * @copyright (c) 2004, 2005 Ian Eure
  19.  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
  20.  * @version   Release: 2.0.2
  21.  * @version   CVS:     $Revision: 1.11 $
  22.  * @link      http://pear.php.net/package/File_Fstab
  23.  */
  24.  
  25. require_once 'PEAR.php';
  26. require_once 'File/Fstab/Entry.php';
  27.  
  28. /**
  29.  * These defines enumerate the possible error types
  30.  */
  31. define('FILE_FSTAB_ERROR_NOENT', -1);
  32. define('FILE_FSTAB_PERMISSION_DENIED', -2);
  33. define('FILE_FSTAB_WRONG_CLASS', -3);
  34.  
  35. /**
  36.  * Class to read, write, and manipulate fstab files
  37.  *
  38.  * @category  File Formats
  39.  * @package   File_Fstab
  40.  * @author    Ian Eure <ieure@php.net>
  41.  * @copyright (c) 2004, 2005 Ian Eure
  42.  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
  43.  * @version   Release: 2.0.2
  44.  * @version   CVS:     $Revision: 1.11 $
  45.  * @link      http://pear.php.net/package/File_Fstab
  46.  */
  47. class File_Fstab {
  48.     /**
  49.      * Array of fstab entries
  50.      *
  51.      * @var array
  52.      */
  53.     var $entries = array();
  54.  
  55.     /**
  56.      * Class options.
  57.      *
  58.      * @var array
  59.      */
  60.     var $options = array();
  61.  
  62.     /**
  63.      * Default options
  64.      *
  65.      * @var array
  66.      * @access private
  67.      */
  68.     var $_defaultOptions = array(
  69.         'entryClass' => "File_Fstab_Entry",
  70.         'file' => "/etc/fstab",
  71.         'fieldSeperator' => "\t"
  72.     );
  73.  
  74.     /**
  75.      * Has the fstab been parsed?
  76.      *
  77.      * @var boolean
  78.      * @access private
  79.      */
  80.     var $_isLoaded = false;
  81.  
  82.     /**
  83.      * Constructor
  84.      *
  85.      * @param   array  $options  Associative array of options to set
  86.      * @return  void
  87.      */
  88.     function File_Fstab($options = false)
  89.     {
  90.         $this->setOptions($options);
  91.         if ($this->options['file']) {
  92.             $this->load();
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Return a single instance to handle a fstab file
  98.      *
  99.      * @param   string  $fstab  Path to the fstab file
  100.      * @return  object  File_Fstab instance
  101.      */
  102.     function &singleton($fstab)
  103.     {
  104.         static $instances;
  105.         if (!isset($instances)) {
  106.             $instances = array();
  107.         }
  108.  
  109.         if (!isset($instances[$fstab])) {
  110.             $instances[$fstab] = &new File_Fstab(array('file' => $fstab));
  111.         }
  112.  
  113.         return $instances[$fstab];
  114.     }
  115.  
  116.     /**
  117.      * Parse fstab file
  118.      *
  119.      * @return  void
  120.      * @since   1.0.1
  121.      */
  122.     function load()
  123.     {
  124.         $fp = fopen($this->options['file'], 'r');
  125.         while ($line = fgets($fp, 1024)) {
  126.  
  127.             // Strip comments & trim whitespace
  128.             $line = trim(ereg_replace('#.*$', '', $line));
  129.  
  130.             // Ignore blank lines
  131.             if (!strlen($line)) {
  132.                 continue;
  133.             }
  134.  
  135.             $class = $this->options['entryClass'];
  136.             $this->entries[] = new $class($line);
  137.  
  138.         }
  139.  
  140.         $this->_isLoaded = true;
  141.     }
  142.  
  143.     /**
  144.      * Update entries
  145.      *
  146.      * This will dump all the entries and re-parse the fstab. There's probably
  147.      * a better way of doing this, like forcing the extant entries to re-parse,
  148.      * and adding/removing entries as needed, but I don't feel like doing that
  149.      * right now.
  150.      *
  151.      * @return  void
  152.      */
  153.     function update()
  154.     {
  155.         unset($this->entries);
  156.         $this->load();
  157.     }
  158.  
  159.     /**
  160.      * Get a File_Fstab_Entry object for a path
  161.      *
  162.      * @param   string  $path  Mount point
  163.      * @return  mixed   File_Fstab_Entry instance on success, PEAR_Error otherwise
  164.      */
  165.     function &getEntryForPath($path)
  166.     {
  167.         foreach ($this->entries as $key => $entry) {
  168.             if ($entry->mountPoint == $path) {
  169.                 // Foreach makes copies - make sure we return a reference
  170.                 return $this->entries[$key];
  171.             }
  172.         }
  173.         return PEAR::raiseError("No entry for path \"{$path}\"", FILE_FSTAB_ERROR_NOENT);
  174.     }
  175.  
  176.     /**
  177.      * Get a File_Fstab_Entry object for a block device
  178.      *
  179.      * @param   string  $blockdev  Block device
  180.      * @return  mixed   File_Fstab_Entry instance on success, PEAR_Error otherwise
  181.      */
  182.     function &getEntryForDevice($blockdev)
  183.     {
  184.         foreach ($this->entries as $key => $entry) {
  185.             if ($entry->getDeviceType() == FILE_FSTAB_ENTRY_DEVTYPE_BLOCKDEV &&
  186.                 $entry->device == $blockdev) {
  187.                 // Foreach makes copies - make sure we return a reference
  188.                 return $this->entries[$key];
  189.             }
  190.         }
  191.         return PEAR::raiseError("No entry for device \"{$blockdev}\"", FILE_FSTAB_ERROR_NOENT);
  192.     }
  193.  
  194.     /**
  195.      * Get a File_Fstab_Entry object for a UUID
  196.      *
  197.      * @param   string  $uuid  UUID device
  198.      * @return  mixed   File_Fstab_Entry instance on success, PEAR_Error otherwise
  199.      */
  200.     function &getEntryForUUID($uuid)
  201.     {
  202.         foreach ($this->entries as $key => $entry) {
  203.             if ($entry->getDeviceType() == FILE_FSTAB_ENTRY_DEVTYPE_UUID &&
  204.                 $entry->uuid == $uuid) {
  205.                 // Foreach makes copies - make sure we return a reference
  206.                 return $this->entries[$key];
  207.             }
  208.         }
  209.         return PEAR::raiseError("No entry for UUID \"{$uuid}\"", FILE_FSTAB_ERROR_NOENT);
  210.     }
  211.  
  212.     /**
  213.      * Get a File_Fstab_Entry object for a label
  214.      *
  215.      * @param   string  $label  Label
  216.      * @return  mixed   File_Fstab_Entry instance on success, PEAR_Error otherwise
  217.      */
  218.     function &getEntryForLabel($label)
  219.     {
  220.         foreach ($this->entries as $key => $entry) {
  221.             if ($entry->getDeviceType() == FILE_FSTAB_ENTRY_DEVTYPE_LABEL &&
  222.                 $entry->label == $label) {
  223.                 // Foreach makes copies - make sure we return a reference
  224.                 return $this->entries[$key];
  225.             }
  226.         }
  227.         return PEAR::raiseError("No entry for label \"{$label}\"", FILE_FSTAB_ERROR_NOENT);
  228.     }
  229.  
  230.     /**
  231.      * Add a new entry
  232.      *
  233.      * @param   object  $entry  Reference to a File_Fstab_Entry-derived class
  234.      * @return  mixed   boolean true on success, PEAR_Error otherwise.
  235.      */
  236.     function addEntry(&$entry)
  237.     {
  238.         if (!is_a($entry, 'File_Fstab_Entry')) {
  239.             return PEAR::raiseError("Entry must be derived from File_Fstab_Entry.",
  240.                                     FILE_FSTAB_WRONG_CLASS);
  241.         }
  242.  
  243.         $this->entries[] = $entry;
  244.         return true;
  245.     }
  246.  
  247.     /**
  248.      * Set class options
  249.      *
  250.      * The allowed options are:
  251.      *
  252.      * - entryClass
  253.      *     Class to use for entries in the fstab. Defaults to File_Fstab_Entry.
  254.      *     you can use this to provide your own entry class with added
  255.      *     functionality. This class must extend File_Fstab_Entry.
  256.      *
  257.      * - file
  258.      *     File to parse. Defaults to /etc/fstab.
  259.      *
  260.      * - fieldSeparator
  261.      *     Separator for fields. This only affects the output when you call
  262.      *     {@link save()}.  This text is placed in between the elements of the
  263.      *     fstab entry line.
  264.      *
  265.      * @param   array  $options  Associative array of options to set
  266.      * @return  void
  267.      */
  268.     function setOptions($options = false)
  269.     {
  270.         if (!is_array($options)) {
  271.             $options = array();
  272.         }
  273.  
  274.         $this->options = array_merge($this->_defaultOptions, $options);
  275.     }
  276.  
  277.     /**
  278.      * Write out a modified fstab
  279.      *
  280.      * WARNING: This will strip comments and blank lines from the original fstab.
  281.      *
  282.      * @return  mixed  true on success, PEAR_Error on failure
  283.      * @since   1.0.1
  284.      */
  285.     function save($output = false)
  286.     {
  287.         $output = $output ? $output : $this->options['file'];
  288.  
  289.         $fp = @fopen($output, 'w');
  290.         if (!$fp) {
  291.             return PEAR::raiseError("Can't write to {$output}",
  292.                                     FILE_FSTAB_PERMISSION_DENIED);
  293.         }
  294.  
  295.         foreach($this->entries as $entry) {
  296.             fwrite($fp, $entry->getEntry($this->options['fieldSeperator'])."\n");
  297.         }
  298.         fclose($fp);
  299.         return true;
  300.     }
  301. }
  302. ?>
  303.